package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TOrder;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TOrderService;
import com.ruipengkj.view.base.BaseDialog;

public class ChangePriceDialog extends BaseDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JLabelJTextFieldComponent number = null;//充值编号
	private JLabelJTextFieldComponent money = null;///金额
	private JLabelJTextFieldComponent changeMoney = null; //更改后金额
	TOrder TOrderAll = new TOrder();
	public boolean isSuccess=false;  //判断是否成功
	private TOrderService tOrderService=BuildSpringApplication.getInstance().getTOrderServiceImpl();
	
	public ChangePriceDialog(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
	}
	private void initListener(){
		okButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		TOrder tOrder = new TOrder();
		String giftMoney=changeMoney.getText().getText().trim();
		
		if(!MatcherFormatUtil.MatcherPriceScheme(giftMoney)){
			JOptionPane.showMessageDialog(null, "赠送金额 不能为空，且小数点左边最多10位数字，右边最多2位数字");
			changeMoney.getText().setText("");
			changeMoney.getText().requestFocus();
			return;
		}else{
			tOrder=TOrderAll;
			tOrder.setNeedMoney(Double.parseDouble(giftMoney));

			if(e.getSource()==okButton){
				//确定
				try {
					tOrderService.saveOrUpdateService(tOrder);
					isSuccess=true;
					//tShopChargeService.saveOrUpdateService(tShopCharge);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					isSuccess=false;
					e1.printStackTrace();
				}
				//BuildSpringApplication.getInstance().getTOpLogServiceImpl().modifyService("");
				this.dispose();
			}
		}
		
	}
	private void initViews() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		number=new JLabelJTextFieldComponent("充值编号：",30);
		money = new JLabelJTextFieldComponent("原 金 额 ：", 30);
		changeMoney=new JLabelJTextFieldComponent("更改金额：",30);
		number.getText().setEditable(false);
		money.getText().setEditable(false);
		JPanel jp1=new JPanel();
		okButton=new JButton("确定");
		jp1.add(okButton);
		
		main_jp.add(number);
		main_jp.add(money);
		main_jp.add(changeMoney);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	public void showDialog(){
		this.setVisible(true);
	}
	public void showDialog(TOrder tOrder){
		if(tOrder!=null){
			TOrderAll=tOrder;
			
			number.getText().setText(tOrder.getOrderID());
			money.getText().setText(tOrder.getNeedMoney()+"");
		}
		this.setVisible(true);
	}
}
